package cn.iocoder.yudao.module.pplan.service.recipemeal;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pplan.controller.admin.recipemeal.vo.RecipeMealPageReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.recipemeal.vo.RecipeMealRespVO;
import cn.iocoder.yudao.module.pplan.controller.admin.recipemeal.vo.RecipeMealSaveReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.recipemealingre.vo.RecipeMealIngreRespVO;
import cn.iocoder.yudao.module.pplan.dal.dataobject.recipemeal.RecipeMealDO;
import cn.iocoder.yudao.module.pplan.dal.dataobject.recipemealingre.RecipeMealIngreDO;
import cn.iocoder.yudao.module.pplan.dal.mysql.recipemeal.RecipeMealMapper;
import cn.iocoder.yudao.module.pplan.dal.mysql.recipemealingre.RecipeMealIngreMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pplan.enums.ErrorCodeConstants.RECIPE_MEAL_NOT_EXISTS;

/**
 * 生产计划-餐谱菜品数据管理 Service 实现类
 *
 * @author yisl
 */
@Service
@Validated
public class RecipeMealServiceImpl implements RecipeMealService {

    @Resource
    private RecipeMealMapper recipeMealMapper;
    @Resource
    private RecipeMealIngreMapper recipeMealIngreMapper;

    @Override
    public Long createRecipeMeal(RecipeMealSaveReqVO createReqVO) {
        // 插入
        RecipeMealDO recipeMeal = BeanUtils.toBean(createReqVO, RecipeMealDO.class);
        recipeMeal.setMealCode("RMEAL-"+System.currentTimeMillis());
        recipeMealMapper.insert(recipeMeal);
        // 插入配料关联信息
        if (ObjectUtil.isNotEmpty(createReqVO.getMealIngreRespVOS())){
            List<RecipeMealIngreDO> recipeMealIngreDOS = BeanUtils.toBean(createReqVO.getMealIngreRespVOS(), RecipeMealIngreDO.class);
            recipeMealIngreDOS.forEach(item -> item.setReceipeMealId(recipeMeal.getId()));
            recipeMealIngreMapper.insertBatch(recipeMealIngreDOS);
        }
        // 返回
        return recipeMeal.getId();
    }

    @Override
    public void updateRecipeMeal(RecipeMealSaveReqVO updateReqVO) {
        // 校验存在
        validateRecipeMealExists(updateReqVO.getId());
        // 更新
        RecipeMealDO updateObj = BeanUtils.toBean(updateReqVO, RecipeMealDO.class);
        recipeMealMapper.updateById(updateObj);
        // 更新配料信息
        recipeMealIngreMapper.delete(RecipeMealIngreDO::getReceipeMealId, updateReqVO.getId());
        if (ObjectUtil.isNotEmpty(updateReqVO.getMealIngreRespVOS())) {
            List<RecipeMealIngreDO> recipeMealIngreDOS = BeanUtils.toBean(updateReqVO.getMealIngreRespVOS(), RecipeMealIngreDO.class);
            recipeMealIngreMapper.insertBatch(recipeMealIngreDOS);
        }
    }

    @Override
    public void deleteRecipeMeal(Long id) {
        // 校验存在
        validateRecipeMealExists(id);
        // 删除
        recipeMealMapper.deleteById(id);
        recipeMealIngreMapper.delete(RecipeMealIngreDO::getReceipeMealId, id);

    }

    @Override
        public void deleteRecipeMealListByIds(List<Long> ids) {
        // 校验存在
        validateRecipeMealExists(ids);
        // 删除
        recipeMealMapper.deleteByIds(ids);
        recipeMealIngreMapper.deleteBatch(RecipeMealIngreDO::getReceipeMealId, ids);
        }

    private void validateRecipeMealExists(List<Long> ids) {
        List<RecipeMealDO> list = recipeMealMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RECIPE_MEAL_NOT_EXISTS);
        }
    }

    private void validateRecipeMealExists(Long id) {
        if (recipeMealMapper.selectById(id) == null) {
            throw exception(RECIPE_MEAL_NOT_EXISTS);
        }
    }

    @Override
    public RecipeMealRespVO getRecipeMeal(Long id) {
        RecipeMealDO recipeMealDO = recipeMealMapper.selectById(id);
        RecipeMealRespVO recipeMealRespVO = BeanUtils.toBean(recipeMealDO, RecipeMealRespVO.class);
        if (ObjectUtil.isNotNull(recipeMealDO)) {
            List<RecipeMealIngreDO> recipeMealIngreDOS = recipeMealIngreMapper.selectList(RecipeMealIngreDO::getReceipeMealId, id);
            if (ObjectUtil.isNotEmpty(recipeMealIngreDOS)) {
                recipeMealRespVO.setMealIngreRespVOS(BeanUtils.toBean(recipeMealIngreDOS, RecipeMealIngreRespVO.class));
            }
        }

        return recipeMealRespVO;
    }

    @Override
    public PageResult<RecipeMealDO> getRecipeMealPage(RecipeMealPageReqVO pageReqVO) {
        return recipeMealMapper.selectPage(pageReqVO);
    }

}